package cn.hd.controller;

import cn.hd.entity.Result;
import cn.hd.entity.Role;
import cn.hd.entity.User;
import cn.hd.service.RoleService;
import cn.hd.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    @RequestMapping("/selectAllRoles")
    public Object selectAllRoles(@RequestParam(defaultValue = "1")Integer pagenum){
        PageInfo<Role> page = roleService.selectAll(pagenum);
        if (page.getList().size()!=0){
            return new Result("200","查询成功",page);
        }
        return new Result("400","查询失败",null);
    }

    @RequestMapping("/selectRolesAll")
    public Object selectRolesAll(){
        List<Role> roles = roleService.selectRole();
        if (roles.size()!=0){
            return new Result("200","查询成功",roles);
        }
        return new Result("400","查询失败",null);
    }

    @RequestMapping("/selectAllRolesBN")
    public Object selectAllRolesBN(Integer pagenum,int pagesize,String roleName){
        Map map = new HashMap();
        map.put("roleName",roleName);
        PageInfo<Role> page = roleService.selectAllBN(pagenum,pagesize,map);
        if (page.getList().size()!=0){
            return new Result("200","查询成功",page);
        }
        return new Result("400","查询失败",null);
    }

    @RequestMapping("/addRoles")
    public Object addRoles(Role role){
        int i = roleService.addRole(role);
        if (i!=0){
            return new Result("200","添加成功",null);
        }
        return  new Result("400","添加失败",null);
    }

    @RequestMapping("/deleteRole")
    public Object deleteRole(long roleId){
        List<User> users = userService.selectByRid(roleId);
        if (users.size() != 0){
            return  new Result("400","删除失败，该角色正在使用",null);
        }else {
            int i = roleService.deleteRole(roleId);
            if (i!=0){
                return new Result("200","删除成功",null);
            }else {
                return new Result("400", "删除失败", null);
            }
        }
    }

    @RequestMapping("/updateRole")
    public Object updateRole(Role role){
        User user = userService.selectByeRid(role.getRoleId());
        if (user != null){
            return  new Result("400","修改失败，该角色正在使用",null);
        }else {
            int i = roleService.updateRole(role);
            if (i!=0){
                return new Result("200","修改成功",null);
            }else {
                return new Result("400", "修改失败", null);
            }
        }
    }

    @RequestMapping("/selectRoleByRid")
    public Object selectRoleByRid(long roleId){
        Role role = roleService.selectByRid(roleId);
        if (role != null){
            return new Result("200","查询成功",role);
        }
        return  new Result("400","查询失败",null);
    }

    //根据角色编号改变其下的所有权限
    @RequestMapping("/updatRolePower")
    public Object updatRolePower(long rid,String pids){
      //根据rid把中间表中的数据删除
        roleService.deleteRolePower(rid);
        //然后遍历pids和rid 加入到中间表
        String[] pid = pids.split(",");
        for (String p:pid){
           roleService.insertRolePower(rid, Long.parseLong(p));
        }
        return new  Result("200","修改成功",null);
    }
    @RequestMapping("/selectUsersByRoleId")
    public Object selectUsersByRoleId(int pagenum,int pagesize,int id){
        PageInfo<User> page = roleService.selectUsersByRoleId(pagenum, pagesize, id);
        if (page.getList().size()!=0){
            return new Result("200","查询成功",page);
        }
        return new Result("400","查询失败",null);
    }




}
